//
//  EditInfoViewController.m
//  SleepCare500
//
//  Created by bchy on 2019/8/8.
//  Copyright © 2019 BoChuang. All rights reserved.
//

#import "EditInfoViewController.h"
//#import "ChooseGenderViewController.h"
#import "LYLPhotoTailoringTool.h"

@interface EditInfoViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIView *footerView;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@property (nonatomic, strong) UIButton *avatarBtn;

@property (nonatomic, strong) UITextField *nameTextField;

@property (nonatomic, strong) UIImage *avatar;


@end

@implementation EditInfoViewController

-(UIButton *)avatarBtn{
    if (!_avatarBtn) {
        _avatarBtn = [[UIButton alloc] initWithFrame:CGRectMake(DEVICE_SCREEN_WIDTH-80, 12.5, 45, 45)];
        [_avatarBtn sd_setImageWithURL:[NSURL URLWithString:[User sharedUser].avatar] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"defaultAvatar"]];
        _avatarBtn.layer.masksToBounds = YES;
        _avatarBtn.layer.cornerRadius = 22.5;
        [_avatarBtn addTarget:self action:@selector(changeAvatar:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _avatarBtn;
}

-(UITextField *)nameTextField{
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(DEVICE_SCREEN_WIDTH-188, 0, 150, 44)];
        _nameTextField.textColor = BLACKTEXTCOLOR;
        _nameTextField.font = [UIFont systemFontOfSize:14];
        _nameTextField.textAlignment = NSTextAlignmentRight;
        _nameTextField.placeholder = @"请输入昵称";
        _nameTextField.text = [User sharedUser].nickname;
    }
    return _nameTextField;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"编辑信息";
    
    self.tableView.tableFooterView = self.footerView;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorColor = SEPARATECOLOR;
    
    _submitBtn.backgroundColor = THEMECOLOR;
    _submitBtn.layer.cornerRadius = 22;
    _submitBtn.layer.shadowOffset = CGSizeMake(2, 2);
    _submitBtn.layer.shadowColor = THEMECOLOR.CGColor;
    _submitBtn.layer.shadowOpacity = 0.4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 80;
    }
    return 44;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.textColor = BLACKTEXTCOLOR;
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"头像";
        [cell.contentView addSubview:self.avatarBtn];
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"昵称";
        [cell.contentView addSubview:self.nameTextField];
    }
   
    return cell;
}



-(void)changeAvatar:(UIButton *)btn{
    
    [[LYLPhotoTailoringTool sharedTool] photoTailoring:^(UIImage *image) {
        [self.avatarBtn setImage:image forState:UIControlStateNormal];
        self.avatar = image;
    }];
    
}


- (IBAction)submit:(id)sender {
    [self.view endEditing:YES];
    if (_nameTextField.text.length == 0) {
        [MBProgressHUD showWarning:@"请输入昵称" toView:nil];
        return;
    }
    if (_avatar) {
        [MBProgressHUD showMessag:@"" toView:self.view];
        [NetWork uploadImageWithImage:_avatar success:^(id responseBody) {
            [self finalSubmitWithAvatarUrl:responseBody[@"img_url"]];
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }else{
        [MBProgressHUD showMessag:@"" toView:self.view];
        [self finalSubmitWithAvatarUrl:[User sharedUser].avatar?[User sharedUser].avatar:@""];
    }
    
}

-(void)finalSubmitWithAvatarUrl:(NSString *)url{
  
    NSDictionary *dict = @{
        @"uid":[User sharedUser].userId,
        @"token": [User sharedUser].token,
                           @"nme": _nameTextField.text,
                           @"img": url,
        @"province":@"",
        @"city":@"",
        @"area":@""
                           };
    NSLog(@"%@", dict);
    
    [NetWork sendGetUrl:@"user/mod_user.php" withParams:dict success:^(id responseBody) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showSuccess:@"编辑成功" toView:nil];
        User *user = [User sharedUser];
        user.avatar = url;
        user.nickname = self.nameTextField.text;
        WZLSERIALIZE_ARCHIVE(user, @"User", userFilePath);
        if (self.editSuccessedBlock) {
            self.editSuccessedBlock();
        }
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}


-(void)finalSubmit{
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

@end
